// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

{% macro per_exec_ws(folder) -%}
  "workspace/exec_${env.EXECUTOR_NUMBER}/{{ folder }}"
{%- endmacro -%}

{% macro sharded_test_step(name, num_shards, node, ws) %}
{% for shard_index in range(1, num_shards + 1) %}
  '{{ name }} {{ shard_index }} of {{ num_shards }}': {
    if (!skip_ci && is_docs_only_build != 1) {
      node('{{ node }}') {
        ws({{ per_exec_ws(ws) }}) {
          try {
            init_git()
            timeout(time: max_time, unit: 'MINUTES') {
              withEnv([
                'TVM_NUM_SHARDS={{ num_shards }}',
                'TVM_SHARD_INDEX={{ shard_index - 1 }}'], {
                {{ caller() | trim | indent(width=12) }}
              })
            }
          } finally {
            junit 'build/pytest-results/*.xml'
          }
        }
      }
    } else {
      Utils.markStageSkippedForConditional('{{ name }} {{ shard_index }} of {{ num_shards }}')
    }
  },
{% endfor %}
{% endmacro %}


{% macro test_step(name, node, ws) %}
  '{{ name }}': {
    if (!skip_ci && is_docs_only_build != 1) {
      node('{{ node }}') {
        ws({{ per_exec_ws(ws) }}) {
          timeout(time: max_time, unit: 'MINUTES') {
            try {
              init_git()
              {{ caller() | indent(width=10) | trim }}
            } finally {
              junit 'build/pytest-results/*.xml'
            }
          }
        }
      }
    } else {
      Utils.markStageSkippedForConditional('{{ name }}')
    }
  },
{% endmacro %}
